<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>筛选表格内容</title>
</head>

<body>
    <!--载入默认美化样式-->
    <link rel="stylesheet" href="https://unpkg.com/@picocss/pico@1.*/css/pico.min.css">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="Application">

        <select v-model="selectedId">
            <option v-for="(item,index) in arrId" :key="index" :value="item">{{item}}</option>
        </select>
        <table>
            <thead>
                <tr>
                    <th>ID</th>

                    <th>时间</th>
                    <th>展现次数</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in filteredData" :key="item.index">
                    <td>{{ item.id }}</td>
                    <td>{{ item.date }}</td>
                    <td>{{ item.count }}</td>
                </tr>
            </tbody>
        </table>

    </div>
    <script>
        const App = Vue.createApp({
            setup() {
                //原始数据数组
                const dataList = [
                    { id: '2', date: '2023-04-20', count: '1' },
                    { id: '3', date: '2023-04-20', count: '8' },
                    { id: '4', date: '2023-04-25', count: '5' },
                    { id: '2', date: '2023-04-20', count: '6' },
                    { id: '2', date: '2023-04-21', count: '2' },
                    { id: '3', date: '2023-04-22', count: '9' },
                ];
                //选择的结果值
                const selectedId = Vue.ref('All');//默认值为All
                //展示的筛选值
                const filteredData = Vue.computed(() => {
                    if (selectedId.value === 'All') {
                        return dataList;
                    } else {
                        const data = dataList.filter(item => item.id === selectedId.value);
                        return data;
                    }
                });

                //拿到ID组成选项数组
                const arrIdFn = () => {
                    // 用 map 方法获取 ID 数组
                    const idList = dataList.map(item => item.id);

                    // 用 Set 数据结构进行去重
                    const uniqueIdList = [...new Set(idList)];

                    // 按从小到大的顺序排序
                    uniqueIdList.sort((a, b) => a - b);
                    //插入all
                    uniqueIdList.unshift("All");
                    return uniqueIdList;
                }
                const arrId = Vue.ref();
                arrId.value = arrIdFn();

                return {
                    selectedId,
                    arrId,
                    filteredData,
                };
            },
        });
        App.mount("#Application");
    </script>
</body>

</html>